package com.k.kc.util.thread.demo1;

import com.k.kc.util.thread.ThreadFactoryBuilder;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class BusinessThreadPool2 {
    private static ThreadPoolExecutor threadPoolExecutor = null;

    private static final int DEFAULT_CORE_POOL_SIZE = 5;
    private static final int DEFAULT_MAXIMUM_POOL_SIZE = 10;
    private static final long DEFAULT_KEEP_ALIVE_TIME = 60;
    private static final TimeUnit DEFAULT_UNIT = TimeUnit.SECONDS;
    private static final int DEFAULT_CAPACITY = 32;
    private static final String DEFAULT_THREAD_NAME_PREFIX = "kpp-thread-";
    private static final int DEFAULT_THREAD_PRIORITY = 1;

    private BusinessThreadPool2(){}

    public static ThreadPoolExecutor getThreadPoolExecutor() {
        if (threadPoolExecutor == null) {
            synchronized (BusinessThreadPool.class) {
                if (threadPoolExecutor == null) {
                    threadPoolExecutor = new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE,
                            DEFAULT_MAXIMUM_POOL_SIZE,
                            DEFAULT_KEEP_ALIVE_TIME,
                            DEFAULT_UNIT,
                            new ArrayBlockingQueue<>(DEFAULT_CAPACITY),
                            new ThreadFactoryBuilder().setNameFormat(DEFAULT_THREAD_NAME_PREFIX).setPriority(DEFAULT_THREAD_PRIORITY).build(),
                            new ThreadPoolExecutor.AbortPolicy()
                    );
                }
            }
        }
        return threadPoolExecutor;
    }
}
